//
//  ViewController.m
//  APP_login
//
//  Created by edz on 7/2/21.
//  Copyright © 2021 baicai. All rights reserved.
//

#import "ViewController.h"
#import "CustomNavigationBar.h"
#import "InformationVC.h"
#import <Masonry.h>

@interface ViewController ()<UITextFieldDelegate>

@property (nonatomic, strong) UILabel *bigTitleLabel;
@property (nonatomic, strong) UILabel *smallTitleLable;
@property (nonatomic, strong) UITextField *phoneNumbTextField;
@property (nonatomic, strong) UITextField *passwordTextField;
@property (nonatomic, strong) UIButton *loginButton;
@property (nonatomic, strong) UIButton *forgetPasswordButton;
@property (nonatomic) BOOL phoneIsOK;
@property (nonatomic) BOOL passwordIsOK;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    _phoneIsOK = NO;
    _passwordIsOK = NO;
    self.view.backgroundColor = [UIColor whiteColor];
    UIBarButtonItem *exitButton =[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemClose target:self action:@selector(exitButtonDidPress)];
    self.navigationItem.leftBarButtonItem = exitButton;
    
    //大文字
    _bigTitleLabel = [[UILabel alloc] init];
    [self.view addSubview:_bigTitleLabel];
    [_bigTitleLabel mas_makeConstraints:^(MASConstraintMaker *make){
        make.left.mas_equalTo(40);
        make.top.mas_equalTo(40);
        make.width.mas_equalTo(140);
        make.height.mas_equalTo(30);
    }];
    _bigTitleLabel.text = @"密码登录";
    _bigTitleLabel.font = [UIFont systemFontOfSize:30];
    
    //小文字
    _smallTitleLable = [[UILabel alloc] init];
    [self.view addSubview:_smallTitleLable];
    [_smallTitleLable mas_makeConstraints:^(MASConstraintMaker *make){
        make.left.mas_equalTo(40);
        make.top.mas_equalTo(85);
        make.width.mas_equalTo(180);
        make.height.mas_equalTo(20);
    }];
    _smallTitleLable.text = @"开始你的学习之旅";
    _smallTitleLable.font = [UIFont systemFontOfSize:20];
    _smallTitleLable.textColor = [UIColor colorWithRed:184/255 green:185/255 blue:186/255 alpha:0.5];
    
    //手机号
    _phoneNumbTextField = [[UITextField alloc] init];
    _phoneNumbTextField.tag = 1010;
    [self.view addSubview:_phoneNumbTextField];
    [_phoneNumbTextField mas_makeConstraints:^(MASConstraintMaker *make){
        make.left.mas_equalTo(40);
        make.top.mas_equalTo(220);
        make.width.mas_equalTo(350);
        make.height.mas_equalTo(45);
    }];
    _phoneNumbTextField.font = [UIFont systemFontOfSize:18];
    //设置下边框
    CALayer *bottomBorder1 = [CALayer layer];
    bottomBorder1.backgroundColor = [UIColor grayColor].CGColor;
    bottomBorder1.frame = CGRectMake(0.0f, _phoneNumbTextField.frame.size.height - 1,
                                    _phoneNumbTextField.frame.size.width, 1.0f);
    bottomBorder1.backgroundColor = [UIColor colorWithRed:244/255 green:245/255 blue:246/255 alpha:0.2].CGColor;
    [_phoneNumbTextField.layer addSublayer:bottomBorder1];
    //位数限制
    [_phoneNumbTextField addTarget:self action:@selector(phoneNumAndPasswordEditingChanged:) forControlEvents:UIControlEventEditingChanged];
    _phoneNumbTextField.keyboardType = UIKeyboardTypeDefault;
    _phoneNumbTextField.placeholder = @"请输入手机号";
    _phoneNumbTextField.delegate = self;
    
    //密码
    _passwordTextField = [[UITextField alloc] init];
    [self.view addSubview:_passwordTextField];
    _passwordTextField.tag = 1011;
    [_passwordTextField mas_makeConstraints:^(MASConstraintMaker *make){
        make.left.mas_equalTo(40);
        make.top.mas_equalTo(280);
        make.width.mas_equalTo(350);
        make.height.mas_equalTo(45);
    }];
    _passwordTextField.font =[UIFont systemFontOfSize:18];
    //设置下边框
    CALayer *bottomBorder = [CALayer layer];
    bottomBorder.frame = CGRectMake(0.0f, _passwordTextField.frame.size.height - 1,
                                    _passwordTextField.frame.size.width, 1.0f);
    bottomBorder.backgroundColor = [UIColor colorWithRed:244/255 green:245/255 blue:246/255 alpha:0.2].CGColor;
    [_passwordTextField.layer addSublayer:bottomBorder];
    [_passwordTextField addTarget:self action:@selector(phoneNumAndPasswordEditingChanged:) forControlEvents: UIControlEventEditingChanged];
    _passwordTextField.keyboardType = UIKeyboardTypeDefault;
    _passwordTextField.placeholder = @"请输入密码(6-20位)";
    _passwordTextField.secureTextEntry = YES;
    _passwordTextField.delegate = self;
    
    //登录
    _loginButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [self.view addSubview:_loginButton];
    [_loginButton mas_makeConstraints:^(MASConstraintMaker *make){
        make.left.mas_equalTo(40);
        make.top.mas_equalTo(400);
        make.width.mas_equalTo(350);
        make.height.mas_equalTo(45);
    }];
    _loginButton.backgroundColor = [UIColor colorWithRed:235/255.0 green:236/255.0 blue:237/255.0 alpha:1];
    _loginButton.titleLabel.font = [UIFont systemFontOfSize:23];
    //设置圆角
    [_loginButton.layer setMasksToBounds:YES];
    [_loginButton.layer setCornerRadius:5.0];
    [_loginButton setTitle:@"登录" forState:UIControlStateNormal];
    [_loginButton setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    [_loginButton addTarget:self action:@selector(loginButtonDidPress) forControlEvents:UIControlEventTouchUpInside];
    _loginButton.enabled = NO;
    
    //忘记密码
    _forgetPasswordButton = [UIButton buttonWithType:UIButtonTypeSystem];
    [self.view addSubview:_forgetPasswordButton];
    [_forgetPasswordButton mas_makeConstraints:^(MASConstraintMaker *make){
        make.left.mas_equalTo(160);
        make.top.mas_equalTo(480);
        make.width.mas_equalTo(100);
        make.height.mas_equalTo(25);
    }];
    [_forgetPasswordButton setTitle:@"忘记密码？" forState:UIControlStateNormal];
    [_forgetPasswordButton addTarget:self action:@selector(forgetPasswordDidPress) forControlEvents:UIControlEventTouchUpInside];
}

//键盘收回
- (void) touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [_phoneNumbTextField resignFirstResponder];
    [_passwordTextField resignFirstResponder];
}

- (void) phoneNumAndPasswordEditingChanged:(UITextField*) textField{
    if (textField.tag == 1010) {
        if(textField.text.length >= 11){
            textField.text = [textField.text substringToIndex:11];
            _phoneIsOK = YES;
        }else{
            _phoneIsOK = NO;
        }
    }
    
    if (textField.tag == 1011) {
        if ( textField.text.length >= 6){
            _passwordIsOK = YES;
            if(textField.text.length >= 12){
                textField.text = [textField.text substringToIndex:12];
            }
        }else{
            _passwordIsOK = NO;
        }
    }
    if ( _passwordIsOK && _phoneIsOK) {
        _loginButton.enabled = YES;
        _loginButton.backgroundColor = [UIColor blueColor];
    }else{
        _loginButton.enabled = NO;
        _loginButton.backgroundColor = [UIColor colorWithRed:235/255.0 green:236/255.0 blue:237/255.0 alpha:1];
    }

}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    if (textField.tag == 1010) {
        return [self validateNumber:string];
    }
    return YES;
}
 
 
- (BOOL)validateNumber:(NSString*)str{
    BOOL res = YES;
    NSCharacterSet* tmpSet = [NSCharacterSet characterSetWithCharactersInString:@"0123456789"];
    int i = 0;
    while (i < str.length) {
        NSString * string = [str substringWithRange:NSMakeRange(i, 1)];
        NSRange range = [string rangeOfCharacterFromSet:tmpSet];
        if (range.length == 0) {
            res = NO;
            break;
        }
        i++;
    }
    return res;
}

- (void) exitButtonDidPress{
    NSLog(@"退出");
}

- (void) loginButtonDidPress{
    InformationVC * info = [[InformationVC alloc] init];
    [self.navigationController pushViewController:info animated:YES];
}

- (void) forgetPasswordDidPress{
    NSLog(@"忘记密码");
}
@end
